//
//  LKNetworkTools.swift
//  AFNetworking单例
//
//  Created by like on 16/8/10.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit
import AFNetworking

//网络请求的方式 枚举
enum LKNetworkToolsMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class LKNetworkTools: AFHTTPSessionManager {
    //单例 全局访问点
    static let sharedTools:LKNetworkTools = {
        let tool = LKNetworkTools()
        
        //"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain"
        
        tool.responseSerializer.acceptableContentTypes?.insert("application/json")
        tool.responseSerializer.acceptableContentTypes?.insert("text/json")
        tool.responseSerializer.acceptableContentTypes?.insert("text/javascript")
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    
    /**
     AFNetwork封装
     
     - parameter method:     请求方式
     - parameter URLString:  URL
     - parameter parameters: 请求参数
     - parameter success:    成功的闭包
     - parameter failure:    失败的闭包
     */
    func request(method:LKNetworkToolsMethod, URLString:String, parameters:AnyObject?, success:(response:  AnyObject?)->(), failure:(error: NSError)->()){
        if method == .GET {
            //get请求
            GET(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        }else{
            //post请求
            POST(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
            
        }
        
    }
    
}
//MARK: - 发布 微博
extension LKNetworkTools{
    
    // 发送文字微博
    func composeUpdate(status: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 请求参数
        let params = [
            "access_token": LKUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        // 发送请求
        request(LKNetworkToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    
    //发送带图片的微博
    func composeUpload(status: String, images:[UIImage], success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        // 请求的urlString
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token": LKUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
            
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "yxyx", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) in
                success(response: response)
                
        }) { (_, error) in
            failure(error: error)
        }
    }
}

//MARK: - 微博首页数据请求
extension LKNetworkTools{
    
    func homeLoadData(sinceId:Int64, maxId:Int64, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = [
            "access_token": LKUserAccountViewModel.sharedTools.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        
        request(LKNetworkToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

//MARK: - 授权界面请求
extension LKNetworkTools{

    func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id":WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        ]
        
        request(LKNetworkToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)

    }
    
    // 请求个人信息
    func oauthLoadUserInfo(userAccount: LKUserAccountModel, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(LKNetworkToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    
    
}


extension LKNetworkTools{
    
    //网络数据的请求   天气信息
    func getWeather(success:(response:  AnyObject?)->(), failure:(error: NSError)->()){
        let url = "http://www.weather.com.cn/data/sk/101010100.html"
        request(LKNetworkToolsMethod.GET, URLString: url, parameters: nil, success: success, failure: failure)
        
    }
    
    
}


